package com.jenvin.activemq.queue;

import java.util.Arrays;
import java.util.Date;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

import com.jenvin.activemq.topic.CommandLineSupport;

public class QueueProducer {

    private String url       = "tcp://192.168.1.62:61616";
    private String queueName = "queue.test";

    public void run() throws JMSException {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
        Connection connection;
        Session session;
        connection = factory.createConnection();
        session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        Destination queue = session.createQueue(queueName);
        MessageProducer producer = session.createProducer(queue);
        connection.start();
        String text = "TextMessage - " + new Date().toLocaleString();
        TextMessage msg = session.createTextMessage(text);
        producer.send(msg);
        System.out.println("publisher message:" + text);
        connection.stop();
        connection.close();
        
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getQueueName() {
        return queueName;
    }

    public void setQueueName(String queueName) {
        this.queueName = queueName;
    }
    public static void main(String[] argv) throws Exception {
        QueueProducer l = new QueueProducer();
        String[] unknown = CommandLineSupport.setOptions(l, argv);
        if (unknown.length > 0) {
            System.out.println("Unknown options: " + Arrays.toString(unknown));
            System.exit(-1);
        }
        l.run();
    }

}
